<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of model_admin
 *
 * @author Admin
 */
class Model_user extends MY_Model {

	var $table = 'user';

	public function create($data) {

		global $phpbb_root_path, $phpEx, $db, $config, $user;
		if (!function_exists('user_add')) {
			include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
		}

		$sql = 'SELECT group_id
			FROM ' . GROUPS_TABLE . "
			WHERE group_name = '" . $db->sql_escape('REGISTERED') . "'
				AND group_type = " . GROUP_SPECIAL;
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		if (!$row) {
			trigger_error('NO_GROUP');
		}

		$group_id = $row['group_id'];

		$user_type = USER_NORMAL;
		$user_actkey = '';
		$user_inactive_reason = 0;
		$user_inactive_time = 0;

		user_add(array(
			'username' => $data['username'],
			'user_password' => phpbb_hash($data['password']),
			'user_email' => $data['email'],
			'group_id' => (int) $group_id,
			'user_timezone' => $config['board_timezone'],
			'user_dst' => $config['board_dst'],
			'user_lang' => $user->lang_name,
			'user_type' => $user_type,
			'user_actkey' => $user_actkey,
			'user_ip' => $user->ip,
			'user_regdate' => time(),
			'user_inactive_reason' => $user_inactive_reason,
			'user_inactive_time' => $user_inactive_time,
		));

		$data['password'] = md5($data['password']);
		$user_id = parent::create($data);
		return $user_id;
	}

	public function update($id, $data) {
		$info = $this->get_info($id);
		global $db;
		$sql_ary = array();
		if ($data['email'] != $info->email) {
			$sql_ary['user_email'] = $data['email'];
			$sql_ary['user_email_hash'] = phpbb_email_hash($data['email']);
		}
		if (isset($data['password']) && md5($data['password']) != $info->password) {
			$sql_ary['user_password'] = phpbb_hash($data['password']);
			$sql_ary['user_passchg'] = time();
			$data['password'] = md5($data['password']);
		}
		if (sizeof($sql_ary)) {
			$sql = 'UPDATE ' . USERS_TABLE . '
				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
				WHERE username = \'' . $db->sql_escape($info->username) . "'";
			$db->sql_query($sql);
		}
		parent::update($id, $data);
	}

	public function del($id) {
		global $db, $phpbb_root_path, $phpEx;
		$info = $this->get_info($id);
		$sql = 'SELECT *
		FROM ' . USERS_TABLE . '
		WHERE username = \'' . $db->sql_escape($info->username) . "'";
		$result = $db->sql_query($sql);
		$user_row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		if (!function_exists('user_delete')) {
			include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
		}

		user_delete('remove', $user_row['user_id']);

		parent::del($id);
	}

}

?>
